package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.CmsArticle;
import com.example.demo.common.Respones;
import com.example.demo.service.CmsArticleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api(tags = "文章管理服务")
@RestController
@RequestMapping("/article")
public class CmsArticleController {
	@Autowired
	private CmsArticleService Articleservice;
	
	@ApiOperation(value = "审核文章")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "artId",value = "文章编号"),
		@ApiImplicitParam(name = "state",value = "状态",
		                  allowableValues = "已发布,已驳回"),
	})
	@PutMapping(value = "/updateState")
	public Respones<String> updateState(String artId,
			String state){
		boolean b=Articleservice.updateState(artId, state);
		if(b) return Respones.ok("操作成功");
		else return Respones.error("要更新的文章不存在");
	}
	
	
	
	//搜索框搜索文章信息
	@ApiOperation(value = "按照条件检索文章信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "begin",value = "开始时间"),
		@ApiImplicitParam(name = "end",value = "结束时间"),
		@ApiImplicitParam(name = "key",value = "文章标题或者作者")
	})
	@GetMapping("/searchBy")
	public Respones<List<CmsArticle>> searchBy(
			Date begin,Date end,String key){
		System.out.println("begin:"+begin);
		System.out.println("end:"+end);
		List<CmsArticle> as = 
				Articleservice.searchBy(begin, end, key);
		return Respones.ok(as);
	}

	//增改
	
	@ApiOperation(value = "保存或者更新文章信息" , 
			notes = "如果给定文章编号，那么代表更新文章信息<br/>"
					+ "如果没有给定文章编号，那么代表新增文章信息")
	@ApiImplicitParams({@ApiImplicitParam(name = "id",value = "文章编号"),
			       @ApiImplicitParam(name = "title",value = "文章标题"),
			       @ApiImplicitParam(name = "author",value = "文章作者"),
			   @ApiImplicitParam(name = "categoryId",value = "所属栏目编号")
			
	})
	@PostMapping("/saveOrUpdateArticle")
	public Respones<String> saveOrUpdate(
			String id,String title,String author,String categoryId){				
			boolean b=
					Articleservice.saveOrUpdateCmsArticle(id, title, author, categoryId);
			if(b) return Respones.ok("操作成功！");
			else return Respones.error("操作失败！");
		
		
	}
	
	
	//查
	@ApiOperation(value = "获取所有文章信息",notes="从数据库检索所有文章信息")
	@GetMapping("/all")
	public Respones<List<CmsArticle>> queryAll(){
		//调用业务方法
		List<CmsArticle> ca=Articleservice.getAllArticle();
		return Respones.ok(ca);
	}

	//单个删除
	@ApiOperation(value = "按照文章编号删除文章信息")
	@ApiImplicitParam(name = "id",value = "文章编号",required = true)
	@DeleteMapping("/deleteById")
	public Respones<String> deleteById(String id){
		int count = Articleservice.deleteById(id);
		return Respones.ok("成功删除" + count +"个文章！");	
	}
	
	
	
	
	
	//批量删除
	@ApiOperation(value = "批量删除文章信息！")
	@DeleteMapping("/deleteByIds")
	public Respones<String> deleteByIds(String[] ids){
		int count = Articleservice.batchDelete(ids);
		return Respones.ok("成功删除"+count+"个文章!");
	}
	

}
